package com.tjetc.output;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.ArrayList;

public class _5ObjectOutputStream {
    /*
        1.使⽤ObjectOutputStream序列化基本数据类型和⼀个Dog对象(name,age),并 保存到data.dat⽂件中。
     */

    // 序列化
    @Test
    public void testObjectOutputStream() throws IOException {
        /*
            注意事项：
            1. 读写顺序要一致
            2. static（修饰类的 序列化只针对对象） 和 transient 修饰的变量不会被序列化
            3. 如果类中包含其他类成员 则也需要实现Serializable 接口才能被序列化
         */
        try (
                ObjectOutputStream objectOutputStream =
                        new ObjectOutputStream(new FileOutputStream("/Users/gaoyongxiang/百度网盘/IDEA-Java/202507-java-se/chapter16/file/data.dat"));
        ) {
            // 报错：java.io.NotSerializableException: com.tjetc.output.Dog
            objectOutputStream.writeObject(new Dog("🐶", 1));
            objectOutputStream.writeInt(1);
            objectOutputStream.writeObject(new Dog("🐶1", 2, 1, new A("gou", 3)));

            objectOutputStream.writeInt(Dog.a);
        }
    }

    // 反序列化
    @Test
    public void testObjectInputStream() throws IOException, ClassNotFoundException {
        try (
                ObjectInputStream objectInputStream =
                        new ObjectInputStream(new FileInputStream("/Users/gaoyongxiang/百度网盘/IDEA-Java/202507-java-se/chapter16/file/data.dat"));
        ) {
            Object o = objectInputStream.readObject();

            if (o instanceof Dog) {
                Dog dog = (Dog) o;
                System.out.println(dog);
            }
            int i = objectInputStream.readInt();
            System.out.println(i);

            Dog dog = (Dog) objectInputStream.readObject();
            System.out.println(dog);

            int num = objectInputStream.readInt();
            System.out.println(num);

        }
    }
}

@SuppressWarnings("all")
class Dog implements Serializable {
    // 强制你改变类结构的时候需要重新生成版本号 否则不匹配
    @Serial
    private static final long serialVersionUID = 5803965461444223751L;
    /*
            1. JVM在序列化的时候会自动生成一个版本号，每次执行序列化都会重新生成
             我们为了确定我们序列化和反序列一致，可以自己编写版本号

    
    
         */
    private String name;
    private Integer age;

    public static int a = 1;
    // transient： 短暂的 被transient修饰的变量 不会被序列化
    transient int b;

    A a1;

    public Dog() {
    }

    public Dog(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Dog(String name, Integer age, int b, A a1) {
        this.name = name;
        this.age = age;
        this.b = b;
        this.a1 = a1;
    }


    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", b=" + b +
                ", a1=" + a1 +
                '}' + a;
    }
}


class A implements Serializable {
    @Serial
    private static final long serialVersionUID = 1219393829971422991L;
    String name;
    int age;

    public A(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "A{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
